home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.4 Applications 1997 August / SGI IRIX 6.4 Applications 1997 August.iso / dist / outbox.idb / var / www / cgi-bin / handler.z / handler
Encoding:
Text File  |  1996-11-14  |  2.4 KB  |  125 lines

  1. #!/usr/bin/perl
  2.  
  3. #__________________________________________________________
  4. #
  5. #   File:    handler
  6. #   By:      Matt Ho
  7. #   Date:    7/23/95
  8. #   Purpose: Appropriately packages documents for download
  9. #            or display.
  10. #__________________________________________________________
  11.  
  12.  
  13. #__________________________________________________________
  14. #
  15. #    Set some environment variables, we'll need through the
  16. #    script and do some initial error checking.
  17. #__________________________________________________________
  18.  
  19. $ROOT     = "/var/www/htdocs" ;    # Root directory
  20. $PATH     = $ENV{'PATH_INFO'} ;
  21.  
  22. $ls       = "/sbin/ls -a1" ;
  23.  
  24. chop $PATH if substr($PATH, -1) eq "/" ;
  25. @_        = split('/', $PATH) ;
  26. $pathRoot = $_[$#_] ;
  27. $doc      = $ROOT.$PATH ;
  28.  
  29. # trim off trailing pipes
  30. $doc =~ s/\|*$// ;
  31.  
  32. &ErrBadPath unless &ValidPath ; # Check for server spoofing
  33.  
  34. #__________________________________________________________
  35. #
  36. #    Read the form data in (we just may need this)
  37. #__________________________________________________________
  38.  
  39. if( $ENV{'REQUEST_METHOD'} eq "GET" )
  40. {
  41.    $buffer=$ENV{'QUERY_STRING'} ;
  42. else
  43. {
  44.    read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'}) ;
  45. }
  46.  
  47. @pairs = split(/&/, $buffer) ;
  48. foreach (@pairs)
  49. {
  50.     tr/+/ / ;    
  51.    ($name,$value) =  split(/=/) ;
  52.     $value        =~ s/%(..)/pack("c",hex($1))/ge ;
  53.     $name         =~ s/%(..)/pack("c",hex($1))/ge ;
  54.  
  55.     $FORM{$name} = $value ;
  56. }
  57.  
  58.  
  59. #__________________________________________________________
  60. #
  61.  
  62. $data = $FORM{'data'} ;
  63. if( $data eq "Download" )
  64. {
  65.     unless( open(INPUT, $doc) )
  66.     {
  67.         print <<ENDOFTEXT ;
  68. Content-type: text/html
  69.  
  70. <HEAD><TITLE>404 Not Found</TITLE></HEAD>
  71. <BODY><H1>404 Not Found</H1>
  72. The requested URL was not found on this server: $ENV{'PATH_INFO'}
  73. <P>
  74. </BODY>
  75. ENDOFTEXT
  76.         return ;
  77.     } 
  78.     print <<ENDOFTEXT ;
  79. Content-type: application/octet-stream
  80.  
  81. ENDOFTEXT
  82.  
  83.     while( read(INPUT, $buf, 16384) )
  84.     {
  85.         print $buf ;
  86.     } 
  87.  
  88.     close(INPUT) ;
  89. }
  90. elsif( $data eq "View" )
  91. {
  92.     substr($PATH, 0, 1) = "/~" ;
  93.     print <<ENDOFTEXT ;
  94. Location: $PATH
  95.  
  96. ENDOFTEXT
  97. }
  98. else
  99. {
  100.     print <<ENDOFTEXT ;
  101. Content-type: text/html
  102.  
  103. <HEAD><TITLE>404 Not Found</TITLE></HEAD>
  104. <BODY><H1>404 Not Found</H1>
  105. The requested URL $PATH was not found on this server.<P>
  106. </BODY>
  107. ENDOFTEXT
  108. }
  109.  
  110. #__________________________________________________________
  111.  
  112. sub ValidPath
  113. {
  114.     return 1 unless /\.\./ ;
  115.    
  116.     return '' if /^\.\./ ;
  117.     return '' if /\/\.\.\// ;
  118.     return '' if /\.\.$/ ;
  119.  
  120.     return 1 ;
  121. }
  122.  
  123.  
  124.